local json=require("cocos.cocos2d.json")
local FZutil=require("app.FZutil")
local Language=require("Language")
local FzFileUtils=require("app.public.FzFileUtils")
local FzSound=require("app.public.FzSound")
local Alert=require("app.public.Alert")
local info=require("info")
local HotUpdaterOld=require("app.HotUpdater.HotUpdaterOld")     

local function reloadlua()
    print("重新加载更新了的lua文件")
    ReloadLua()
    require("app.FzInit")       --重头加载
    local MyApp=require("app.MyApp"):create()
    cc.exports.MyApp=MyApp
end

local function getSizeStr( size )
    local size_type="bytes"
    local num=size
    if size>1024*1024 then
        num=size/1024/1024
        size_type="MB"
    elseif size>1024 then
        num=size/1024
        size_type="KB"
    end
    return num,size_type
end

local MainScene = class("MainScene", cc.load("mvc").ViewBase)
function MainScene:onCreate()
    cc.exports.FzSound=FzSound
    
    local uimap=require("app.Login.BackgroundLayer").create()
    self:addChild(uimap.root)
    self.uimap=uimap
    
    self.updater=HotUpdaterOld.new(self)

    self:init()
end--end function onCreate

function MainScene:init()
    self:initNotice()
    if SKIP_HOT_UPDATE then
        return
    end
    self:initHotUpdateManager()

--    local server_list_buf=FzFileUtils.readFile(FzFileUtils.getServerListPath())
--    local server_list_data=json.decode(server_list_buf)
--    self:initServers(server_list_data)
--    self.server_list_data=server_list_data
--    self:updateNotice(server_list_data)
    self:initVerision()
end

function MainScene:initVerision()
    local versionBuf=cc.FileUtils:getInstance():getStringFromFile("project.manifest")
    assert(string.len(versionBuf)>0,"project.manifest，读取失败")
    local versionJson=json.decode(versionBuf)
    assert(versionJson,"project.manifest解析失败")
    local version=versionJson.version
    local txt=string.format(Language.Version_str .. version .. "<" .. info.cpp_version .. ">")
    local ver_label=cc.Label:createWithTTF(txt,"SIMHEI.TTF",22)
    self:addChild(ver_label)
    ver_label:setPosition(display.width-30,52)
    ver_label:setAnchorPoint(1,0)
    FZutil.setAllTextOutLine(ver_label)
end

function MainScene:onCleanup()
    self.updater:onCleanup()
end

function MainScene:initServers(server_list_data)
    local new_servers={}
    for key, var in pairs(server_list_data.server) do
    	new_servers[tonumber(key)]=var
    	var.server_id=tonumber(key)
    end
    server_list_data.server=new_servers
end


function MainScene:initNotice()
	local notice=FZutil.createSceneNode("LoginUIall/LoginUIView_announcement")
	self:addChild(notice.root)
	self.notice=notice
	notice.root:setPosition(display.cx,display.cy)
	notice.Text_content_N:setString("Loading...")
    local txt=notice.Text_content_N
    txt:hide()
    local scrollview=self.notice.ScrollView
    local scsize=scrollview:getContentSize()
    local msg_txt=cc.Label:createWithTTF("Loading...","SIMHEI.TTF",txt:getFontSize(),cc.size(scsize.width-30,0))
    msg_txt:setLineBreakWithoutSpace(true)
    msg_txt:setAnchorPoint(0.5,0.5)
    local size=msg_txt:getContentSize()
    msg_txt:getPosition(scsize.width/2,scsize.height-size.height/2)
    scrollview:addChild(msg_txt)
    notice.msg_txt=msg_txt
	--notice.Button_confirm:hide()
    notice.Button_confirm:show()
	notice.Button_confirm:onTouch(function(e)
	   if e.name=="ended" then
	   	   notice.root:removeSelf()
           if SKIP_HOT_UPDATE then
                reloadlua()
                self:EnterGame()
                return
            end
           self:checkUpdate()
	   end
	end)
end

function MainScene:checkUpdate()
    self.updater:checkUpdate()
end

function MainScene:initHotUpdateManager()
    self.updater:initHotUpdateManager()
end

function MainScene:updateNotice(server_list_data)
    local txt=self.notice.msg_txt
    txt:setString(server_list_data.notice)
    local size=txt:getContentSize()
    local scrollview=self.notice.ScrollView
    local scsize=scrollview:getContentSize()
    if size.height>scsize.height+20 then
    	scrollview:setInnerContainerSize(cc.size(scsize.width,size.height+20))
    	txt:setPosition(scsize.width/2,size.height/2+10)
    else    
        scrollview:setInnerContainerSize(scsize)
        txt:setPosition(scsize.width/2,scsize.height-size.height/2-10)
    end
    self.notice.Button_confirm:show()
end

function MainScene:getUpdateNode()
    if not self.updateMap then
        local updateMap=FZutil.createSceneNode("LoginUIall/LoginUIView_update")
        self:addChild(updateMap.root)
        updateMap.root:setPosition(display.cx,display.cy-200)
        self.updateMap=updateMap
        updateMap.LoadingBar:setPercent(0)
        updateMap.Text_process:setString("0%")
        updateMap.Button_update:onTouch(function(e)
            if e.name=="ended" then
                self:checkUpdate()
            end
        end)
        if updateMap.Button_Clean then 
            updateMap.Button_Clean:onTouch(function( e )
                if e.name=="ended" then
                    Alert:showForce(Language.clean_title,Language.clean_content,Alert.TYPE_YES_NO,function( isYes )
                        if isYes then 
                            FzFileUtils.deleteDir(FzFileUtils.getDownloadPath())
                            cc.Director:getInstance():resume()
                            cc.Director:getInstance():getScheduler():setTimeScale(1)
                            MyApp:restart()
                        end
                    end)
                end 
            end)
        end
	end
	return self.updateMap
end

function MainScene:EnterGame()
    --FzSound.ChangeMusic(FzSound.music.main_name)
    local login_scene=MyApp:enterScene("LoginScene")
    --login_scene:setServerList(self.server_list_data)
end

return MainScene
